<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('报告期管理') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.native.prevent>
      <el-form-item :label="$t('年份')" prop="year">
        <el-date-picker
          v-model="queryParams.year"
          type="year"
          value-format="yyyy"
          :placeholder="$t('选择年')"
        />
      </el-form-item>
      <el-form-item :label="$t('报告期')" prop="reportPeriod">
        <el-select v-model="queryParams.reportPeriod" placeholder="请选择">
          <el-option
            v-for="item in reportPeriodOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}
        </el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" tooltip-effect="darkTable" :data="bfReportPeriodManagementList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('年份')" align="center" prop="year" />
      <el-table-column :label="$t('报告期')" align="center" prop="reportPeriod" />
      <el-table-column :label="$t('填报完成')" align="center" prop="completedCount">
        <template slot-scope="scope">
          <span class="link-type">{{ scope.row.completedCount }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('已提交待审核')" align="center" prop="reviewedCount">
        <template slot-scope="scope">
          <span class="link-type">{{ scope.row.reviewedCount }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('未提交')" align="center" prop="unSubmittedCount">
        <template slot-scope="scope">
          <span class="link-type">{{ scope.row.unSubmittedCount }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="280">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
          >{{ $t('修改') }}
          </el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row, scope.$index)"
          >{{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :visible.sync="formOpen" :title="$t('报告期管理')" :close-on-click-modal="false" width="50%">
      <el-form ref="bfPortalAccountForm" :model="form" label-width="120px" class="dataForm">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('年份')" prop="year">
              <el-date-picker
                v-model="form.year"
                type="year"
                value-format="yyyy"
                :placeholder="$t('选择年')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('报告期')" prop="accountName">
              <el-select v-model="form.reportPeriod" placeholder="请选择">
                <el-option
                  v-for="item in reportPeriodOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="formOpen = false">{{ $t('取消') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('确定') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  data() {
    return {
      // 填报数据遮罩层
      loading: true,
      // 填报数据总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      formOpen: false,
      // 报告期管理表格数据
      bfReportPeriodManagementList: [],
      // 查询参数
      queryParams: {
        gpName: undefined,
        year: undefined,
        type: undefined,
        status: 'Y',
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      reportPeriodOptions: [
        {
          label: 'Q1',
          value: 'Q1'
        },
        {
          label: 'Q2',
          value: 'Q2'
        },
        {
          label: 'Q3',
          value: 'Q3'
        },
        {
          label: 'Q4',
          value: 'Q4'
        }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询填报数据审核列表 */
    getList() {
      this.loading = true
      this.bfReportPeriodManagementList = [
        {
          id: crypto.randomUUID(),
          year: '2023',
          reportPeriod: 'Q4',
          completedCount: 0,
          reviewedCount: 2,
          unSubmittedCount: 0
        },
        {
          id: crypto.randomUUID(),
          year: '2023',
          reportPeriod: 'Q3',
          completedCount: 2,
          reviewedCount: 0,
          unSubmittedCount: 0
        },
        {
          id: crypto.randomUUID(),
          year: '2023',
          reportPeriod: 'Q2',
          completedCount: 2,
          reviewedCount: 0,
          unSubmittedCount: 0
        }
      ]
      this.loading = false
    },
    // 表单重置
    reset() {
      this.form = {
        gpName: undefined,
        accountName: undefined,
        fillInFund: undefined,
        status: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.title = this.$t('报告期管理')
      this.reset()
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.title = this.$t('报告期管理')
      this.form = row
    },
    /** 删除按钮操作 */
    deleteHandle(row, index) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return true
      }).then(() => {
        this.bfReportPeriodManagementList.splice(index, 1)
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    statusChangeHandle(row) {
      // 原始状态
      const originalStatus = row.status === 'Y' ? 'N' : 'Y'
      const statusFormat = this.statusFormat(row.status)
      this.$confirm(this.$t('此操作将 "' + statusFormat + '", 是否继续?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        // 确认后执行状态改变
        return this.msgSuccess(this.$t(statusFormat + '成功'))
      }).catch(() => {
        // 取消后重置状态
        row.status = originalStatus
      })
    },
    statusFormat(status) {
      return status === 'Y' ? '开启' : '关闭'
    },
    initGpName(val) {
      this.form.gpName = val.name
    },
    submitForm() {
      this.loading = true
      if (this.form.id) {
        // eslint-disable-next-line no-unused-vars
        let accountInfo = this.bfReportPeriodManagementList.find(val => val.id === this.form.id)
        accountInfo = this.form
      } else {
        this.form.id = crypto.randomUUID()
        this.form.completedCount = 0
        this.form.reviewedCount = 0
        this.form.unSubmittedCount = 0
        this.bfReportPeriodManagementList.unshift(this.form)
      }
      this.loading = false
      this.formOpen = false
    }
  }
}
</script>
